#include "MonitoringForgotPassword.h"
#include "ui_MonitoringForgotPassword.h"

MonitoringForgotPassword::MonitoringForgotPassword(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MonitoringForgotPassword),
    userSqlite(MonitoringUserSqlite::getInstance())
{
    ui->setupUi(this);
    timer=new QTimer(this);
    connect(timer, &QTimer::timeout, this,&MonitoringForgotPassword::timerFinish);
    //修改密码
    connect(ui->btnForgot,&QPushButton::clicked,this,[=](){
        LOG_INFO("点击了修改密码");
        if(!MonitoringTool::isPhoneNumberValid(ui->lineEditUserName_2->text())){

            timer->start(3000);
            ui->label_3->setText("手机号格式不正确");
            return ;
        }
        if(ui->lineEditPassword_2->text().isEmpty()){
            timer->start(3000);
            ui->label_3->setText("密码不能为空");
            return ;
        }
        LOG_INFO("用户"+model->id+"修改了忘记密码");
        if (SnaillTcpClient::instance()->sendJsonData(MonitoringTool::getUserJson("forgot",ui->lineEditUserName_2->text(),ui->lineEditPassword_2->text(),"",""))) {
                   //  qDebug() << "Login data sent successfully";
               } else {
                   ui->label_3->setText("登录失败，请检查网络");
                   return ;
               }
//        if(userSqlite.changePassword(ui->lineEditUserName_2->text(),ui->lineEditPassword_2->text())==QUERY_SUCCESS_USER_NOT_EXISTS){
//            timer->start(3000);
//            ui->label_3->setText("用户不存在");
//            return ;
//        }
//        if(userSqlite.changePassword(ui->lineEditUserName_2->text(),ui->lineEditPassword_2->text())==QUERY_FAILED){
//            timer->start(3000);
//            ui->label_3->setText("数据库操作失败");
//            return ;
//        }
//        emit forgotClick();
});
    connect(SnaillTcpClient::instance(), &SnaillTcpClient::dataReceived, this, &MonitoringForgotPassword::handleDataReceived);
    connect(ui->pushButton,&QPushButton::clicked,this,[=](){

        emit backClick();
    });
}
void MonitoringForgotPassword::handleDataReceived(const QByteArray &data)
{
    qDebug()<<"==+++=="<<data;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(data);
    QJsonObject jsonObj = jsonDoc.object();
    QString action = jsonObj["action"].toString();
    if(action=="forgot"){
        QString coded = jsonObj["code"].toString();
        int code = coded.toInt();
        if(code==QUERY_SUCCESS_USER_NOT_EXISTS){
            timer->start(3000);
            ui->label_3->setText("用户不存在");
            return ;

        }
        if(code==QUERY_FAILED){
            timer->start(3000);
            ui->label_2->setText("数据库操作失败");
            return ;
        }
       emit forgotClick();
    }


}
void MonitoringForgotPassword::timerFinish()
{
    ui->label_3->clear();
}
MonitoringForgotPassword::~MonitoringForgotPassword()
{
    delete ui;
    delete timer;
}
